(function(){
    var factory = {
        pollingTime: 3000,    //轮播时间间隔
        init: function(){
            var _this = this;
            _this.scrollLoadingPic();

            /*导航定位*/
            _this.navPosition();

            //首页banner焦点幻灯片
            _this.banner();

            //产品服务3D轮播
            _this.proService();

            //特色服务
            _this.speService();

            //返回顶部
            _this.goBack();

            //在线咨询或留言
            _this.onlineIM();
        },
        scrollLoadingPic:function () {
            $("img.lazy").lazyload();
        },
        navPosition: function(){    //导航定位
            $('.menu-list a.list-link').click(function(){
                var hash = $(this).attr('data-hash');
                if(hash){
                    var scrollTo = $(hash).offset().top;
                    if(hash = '#product'){
                        scrollTo = scrollTo + 60;
                    }
                    $('body,html').animate({scrollTop:scrollTo},500);
                }
            });
        },
        banner: function(){    //首页banner焦点幻灯片
            var _this = this;
            var opts = {
                'time': _this.pollingTime //轮播时间间隔
            };

            function plugin(elm, options){
                var _this = this;

                _this.$elm = $(elm);

                opts = $.extend(opts, options||{});

                _this.init();
            }

            plugin.prototype = {
                init: function(){
                    var _this = this;

                    opts.idx = 1;    //初始化几秒后显示第一张图

                    opts.lengths = _this.$elm.find(opts.btnBox).find(opts.btnDom).length; //长度

                    //视图事件监听
                    _this.handle();
                },
                handle: function(){
                    var _this = this;

                    function polling(){
                        if(opts.idx >= opts.lengths){
                            opts.idx = 0;
                        }

                        _this.$elm.find(opts.btnBox).find(opts.btnDom).eq(opts.idx).addClass(opts.activeClass).siblings().removeClass(opts.activeClass);

                        _this.$elm.find(opts.imgBox).find(opts.imgDom).eq(opts.idx).addClass(opts.activeClass).siblings().removeClass(opts.activeClass);

                        ++opts.idx;
                    }

                    function pollingTimer(){
                        return setInterval(polling, opts.time);
                    }

                    opts.timeHandle = pollingTimer();

                    _this.$elm.hover(function(){
                        clearTimeout(opts.timeHandle);
                    },function(){
                        opts.timeHandle = pollingTimer();
                    });

                    _this.$elm.find(opts.btnBox).find(opts.btnDom).hover(function(e){
                        opts.idx = $(this).index();

                        polling();

                        clearTimeout(opts.timeHandle);

                        e.stopPropagation();
                    });
                }
            };

            $.fn.hslide = function(options){
                return new plugin(this, options);
            };

            /*banner轮播*/
            $('.m-slide').hslide({
                'time': _this.pollingTime,
                'imgBox': '.slide-img',
                'imgDom': 'li',
                'btnBox': '.slide-btn',
                'btnDom': 'a',
                'activeClass': 'active'
            });
        },
        speService: function(){    //特色服务
            $('#special .m-special-btn a').hover(function(){
                var index = $(this).index();

                $(this).addClass('active').siblings().removeClass('active');
                if(index == 0){
                    $('#special .wrap-special-list').animate({'left':'0px'});
                }else if(index == 1){
                    $('#special .wrap-special-list').animate({'left':'-340px'});
                }
            });
        },
        goBack: function(){    //返回顶部
            $(window).scroll(function(){
                var scrollTop = $(window).scrollTop();
                if(scrollTop > 0){
                    $("#go-back").slideDown();
                }else{
                    $("#go-back").slideUp();
                }
            });
            // $(window).scroll();
            $("#go-back").on('click', function(){
                $(document).scrollTop(0);
                // $('body,html').animate({ scrollTop: 0 }, 500);
            });
        },
        onlineIM: function(){   //在线咨询

        },
        initIm: function(data){
            var html = '<li class="chatmsg chatmsg-left clearfix"><p className="service-time">'+ data.createTime +'</p><div class="service-msg"><div class="u-msg"><pre>欢迎您，请问有什么可以帮到您？</pre></div></div></li>';
            $('#m-im .im-msg ul').html(html);

            $('#m-im .im-enter textarea').val('');
        },
        showIM: function(){
            $('#m-im').slideDown();
        },
        hideIM: function(){
            $('#m-im').slideUp();
        },
        addImMsg: function(data, msg){
            var html = '<li class="chatmsg chatmsg-right clearfix">';
            html += '<p class="service-time">' + data.createTime + '</p>';
            html += '<div class="service-msg">';
            html += '<div class="u-msg"><pre>' + msg + '</pre></div>';
            html += '</div>';
            html += '</li>';

            $('#m-im .im-msg ul').append(html);
            $('#m-im .im-enter textarea').val('');
            $('#im-msg').scrollTop($('#im-msg')[0].scrollHeight);
        },
        proService: function(){    //产品服务3D轮播
            var speed = this.pollingTime;

            var $wrap = $('.card-wrap'),
                widthWrap = $wrap.outerWidth();

            var $items = $('.card-item'),
                width = $items.outerWidth();

            var n = $items.length;

            if(n < 4){ return ; }

            var on = true;  // 点击切换开关,false为不允许切换

            var posArr = [], indexArr = [], oldPosArr = [];
            for(var i=1; i<=n; i++){
                indexArr.push(i);

                if(i == 1){
                    posArr.push(0);
                    oldPosArr.push(0);
                }else if(i == 2){
                    posArr.push((widthWrap-width)/2);
                    oldPosArr.push((widthWrap-width)/2);
                }else if(i == 3){
                    posArr.push(widthWrap-width);
                    oldPosArr.push(widthWrap-width);
                }else if(i == n){
                    posArr.push(-width);
                    oldPosArr.push(-width);
                }else{
                    posArr.push(widthWrap+width*(i-2));
                    oldPosArr.push(widthWrap+width*(i-2));
                }
            }

            $items.each(function(index, item){
                var scale = 0.85;
                if($(item).hasClass('active')){
                    scale = 1;
                }
                $(item).css('-ms-transform', 'translateX(' + posArr[index] + 'px) scale('+ scale +')');
                $(item).css('-webkit-transform', 'translateX(' + posArr[index] + 'px) scale('+ scale +')');
                $(item).css('transform', 'translateX(' + posArr[index] + 'px) scale('+ scale +')');
            });

            // 布局好之后显示
            $('.card-wrap').css({opacity: 1});
            $(document).on('click', '.card-wrap .card-item', function(){
                // 不允许点击切换
                if(!on){ return; }

                var index = indexArr[$(this).index()];
                if(index === 1){    //向左
                    posArr.push(posArr.shift(posArr[0]));
                    indexArr.push(indexArr.shift(indexArr[0]));
                }else if(index === 3){    //向右
                    posArr.unshift(posArr.pop(posArr[0]));
                    indexArr.unshift(indexArr.pop(indexArr[0]));
                }else{
                    return;
                }
                polling(true);
            });

            var timer = setInterval(polling, speed);

            $items.hover(function(){
                clearTimeout(timer);
            },function(){
                timer = setInterval(polling, speed);
            });

            function polling(isClick){
                if(!isClick){
                    posArr.unshift(posArr.pop(posArr[0]));
                    indexArr.unshift(indexArr.pop(indexArr[0]));
                }

                on = false;   // 已经开始切换，不允许点击切换
                $items.each(function(index, item){
                    if(indexArr[index] > 4 && indexArr[index]< n){
                        $(item).css('opacity', 0);
                    }else{
                        $(item).css('opacity', 1);
                    }

                    $(item).removeClass('active');
                    if(indexArr[index] == 2){
                        $(item).addClass('active');
                    }

                    var scale = 0.85;
                    if($(item).hasClass('active')){
                        scale = 1;
                        var idx = $(item).index();

                        if(idx < 4){
                            $('.product-title h2').eq(0).addClass('active').siblings().removeClass('active');
                        }else if(idx >= 4 && idx <7){
                            $('.product-title h2').eq(1).addClass('active').siblings().removeClass('active');
                        }else if(idx >= 7 && idx <11){
                            $('.product-title h2').eq(2).addClass('active').siblings().removeClass('active');
                        }else if(idx >= 11){
                            $('.product-title h2').eq(3).addClass('active').siblings().removeClass('active');
                        }
                    }
                    $(item).css('-ms-transform', 'translateX(' + posArr[index] + 'px) scale('+ scale +')');
                    $(item).css('-webkit-transform', 'translateX(' + posArr[index] + 'px) scale('+ scale +')');
                    $(item).css('transform', 'translateX(' + posArr[index] + 'px) scale('+ scale +')');

                    setTimeout(function(){
                        on = true;
                    }, 600);
                });
            }

            $('.product-title h2').click(function(){
                var index = $(this).index();
                if(index == 0){
                    indexArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
                }else if(index == 1){
                    indexArr = [11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
                }else if(index == 2){
                    indexArr = [8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7];
                }else if(index == 3){
                    indexArr = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3];
                }
                posArr = [];
                for(var i=0, l=indexArr.length; i<l; i++){
                    posArr.push(oldPosArr[indexArr[i]-1]);
                }
                timer && clearTimeout(timer);
                polling(true);
                timer = setInterval(polling, speed);
            });
        }
    };

    module.exports = factory;
})();
